/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package user.permissions;

/**
 *
 * @author gilberto_CIDITIC
 */
/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
import java.sql.SQLException;
import java.util.*;
import javax.el.ELContext;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import system.general.Exc;
import system.general.Logged;
import system.general.Message;
import system.general.Persistencia;

/**
 *
 * @author gilberto_CIDITIC
 */
@ManagedBean(name = "user_permission")
@RequestScoped
public final class Main extends Beans {

    //Variables de importación General
    Logged logged;
    private Persistencia persistencia = new Persistencia();
    private Message msg = new Message();
    //Variables de importación otros modulos
    //Variables del modulo
    private Find person_permissionFind = new Find();
    private List<Beans> person_permissionList = new ArrayList<>();
    private List<Beans> moduleList = new ArrayList<>();
    //Variables locales
    private String[] Modulo = new String[51];
    private String sql;
    private Map<String, String> map = new LinkedHashMap<>();
    

    public Main() {
        
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        logged = (Logged) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "logged");
        load();
    }

    public void load() {
        try {
            String moduleNumber = "";
            String moduleName = "";

            if (logged.getMasterId() == 0) {
                return;
            }
            clear();

            //Recorre el archivo general.module.properties para listar los modulos
            //contemplados en el sistemas
            Properties mp = leerProperties();
            for (Enumeration e = mp.keys(); e.hasMoreElements();) {
                Object obj = e.nextElement();
                String moduleKey = obj.toString();
                if (moduleKey.endsWith("access")) {
                    for (int i = 0; i < moduleKey.length(); i++) {
                        if (Character.isDigit(moduleKey.charAt(i))) {
                            moduleNumber = moduleNumber + moduleKey.charAt(i);
                        }
                    }
                    moduleName = mp.getProperty(moduleKey);

                    //localiza el tipo de acceso de la persona logueada para el modulo actual
                    map.clear();
                    map.put("id_persona = " + logged.getMasterId(), " AND ");
                    map.put("nombre = '" + moduleName, "' ");
                    person_permissionList = person_permissionFind.byMap(map);
                    if (!person_permissionList.isEmpty()) {
                        this.Modulo[Integer.parseInt(moduleNumber)] = person_permissionList.get(0).getAcceso().toString();
                    }
                    moduleList.add(new Beans(0, 0, moduleKey, 0, "module" + moduleNumber + "label", "module" + moduleNumber + "detail"));
                    moduleNumber = "";
                }

            }

        } catch (Exc ex) {
            msg.showMsg(namePackage(), "system", ex.getIdError());
        } catch (SQLException ex) {
            msg.showErr(namePackage(), "sql", ex);
        } catch (Exception ex) {
            msg.showErr(namePackage(), "java", ex);
        } finally {
        }
    }

    public void update() {
        try {

            String moduleNumber = "";
            String moduleName = "";

            //Verifica si hay registros seleccionados
            if (logged.getMasterId() == 0) {
                throw new Exc("m4");
            }
            sql = "DELETE FROM public.persona_permiso "
                    + "WHERE id_persona = " + logged.getMasterId();

            //Borra todos los permisos del usuario
            persistencia.executeUpdate(sql);

            Properties mp = leerProperties();
            for (Enumeration e = mp.keys(); e.hasMoreElements();) {
                Object obj = e.nextElement();
                String moduleKey = obj.toString();
                if (moduleKey.endsWith("access")) {
                    for (int i = 0; i < moduleKey.length(); i++) {
                        if (Character.isDigit(moduleKey.charAt(i))) {
                            moduleNumber = moduleNumber + moduleKey.charAt(i);
                        }
                    }
                    moduleName = mp.getProperty(moduleKey);

                    sql = "INSERT INTO public.persona_permiso (id_persona, nombre, acceso) "
                            + "VALUES (" + logged.getMasterId() + ",'" + moduleName + "'," + Modulo[Integer.parseInt(moduleNumber)] + ")";

                    //Inserta la información del usuario
                    if (!Modulo[Integer.parseInt(moduleNumber)].equals("0")) {
                        persistencia.executeUpdate(sql);
                    }
                }
                moduleNumber = "";
            }
            throw new Exc("m2");
        } catch (Exc ex) {
            msg.showMsg(namePackage(), "system", ex.getIdError());
        } catch (SQLException ex) {
            msg.showErr(namePackage(), "sql", ex);
        } catch (Exception ex) {
            msg.showErr(namePackage(), "java", ex);
        } finally {
        }

    }

    public void delete() {
        try {

            //Verifica si hay registros seleccionados
            if (logged.getMasterId() == 0) {
                throw new Exc("m4");
            }

            //Elimina los persimos de la persona seleccionada
            deleteByIdPersona(logged.getMasterId());
            throw new Exc("m3");

        } catch (Exc ex) {
            msg.showMsg(namePackage(), "system", ex.getIdError());
        } catch (SQLException ex) {
            msg.showErr(namePackage(), "sql", ex);
        } catch (Exception ex) {
            msg.showErr(namePackage(), "java", ex);
        } finally {
        }
    }

    public void deleteByIdPersona(int id_persona) throws Exc, SQLException, Exception {

        //Verifica si el usuario ya existe para otra persona
        sql = "DELETE FROM public.persona_permiso "
                + "WHERE id_persona = " + id_persona;

        persistencia.executeUpdate(sql);
    }

    public void clear() {
        for (int i = 1; i < Modulo.length; i++) {
            this.Modulo[i] = "0";
        }
    }

    public Properties leerProperties() {
        Properties propiedades = new Properties();
        try {
            propiedades.load(getClass().getResourceAsStream("/system/general/label_es.properties"));
            if (propiedades.isEmpty()) {
                return null;
            }
        } catch (Exception ex) {
            msg.showErr(namePackage(), "java", ex);
        } finally {
            return propiedades;
        }
    }

    public List<Beans> getModules() {
        return moduleList;
    }

    //SETTERS AND GETTERS
    /*
     * set y get del modulo 1
     */
    public void setModule1access(String mod) {
        this.Modulo[1] = mod;
    }

    public String getModule1access() {
        return this.Modulo[1];
    }

    /*
     * set y get del modulo 2
     */
    public void setModule2access(String mod) {
        this.Modulo[2] = mod;
    }

    public String getModule2access() {
        return this.Modulo[2];
    }

    /*
     * set y get del modulo 3
     */
    public void setModule3access(String mod) {
        this.Modulo[3] = mod;
    }

    public String getModule3access() {
        return this.Modulo[3];
    }

    /*
     * set y get del modulo 4
     */
    public void setModule4access(String mod) {
        this.Modulo[4] = mod;
    }

    public String getModule4access() {
        return this.Modulo[4];
    }

    /*
     * set y get del modulo 5
     */
    public void setModule5access(String mod) {
        this.Modulo[5] = mod;
    }

    public String getModule5access() {
        return this.Modulo[5];
    }

    /*
     * set y get del modulo 6
     */
    public void setModule6access(String mod) {
        this.Modulo[6] = mod;
    }

    public String getModule6access() {
        return this.Modulo[6];
    }

    /*
     * set y get del modulo 7
     */
    public void setModule7access(String mod) {
        this.Modulo[7] = mod;
    }

    public String getModule7access() {
        return this.Modulo[7];
    }

    /*
     * set y get del modulo 8
     */
    public void setModule8access(String mod) {
        this.Modulo[8] = mod;
    }

    public String getModule8access() {
        return this.Modulo[8];
    }

    /*
     * set y get del modulo 9
     */
    public void setModule9access(String mod) {
        this.Modulo[9] = mod;
    }

    public String getModule9access() {
        return this.Modulo[9];
    }

    /*
     * set y get del modulo 10
     */
    public void setModule10access(String mod) {
        this.Modulo[10] = mod;
    }

    public String getModule10access() {
        return this.Modulo[10];
    }

    /*
     * set y get del modulo 11
     */
    public void setModule11access(String mod) {
        this.Modulo[11] = mod;
    }

    public String getModule11access() {
        return this.Modulo[11];
    }

    /*
     * set y get del modulo 12
     */
    public void setModule12access(String mod) {
        this.Modulo[12] = mod;
    }

    public String getModule12access() {
        return this.Modulo[12];
    }

    /*
     * set y get del modulo 13
     */
    public void setModule13access(String mod) {
        this.Modulo[13] = mod;
    }

    public String getModule13access() {
        return this.Modulo[13];
    }

    /*
     * set y get del modulo 14
     */
    public void setModule14access(String mod) {
        this.Modulo[14] = mod;
    }

    public String getModule14access() {
        return this.Modulo[14];
    }

    /*
     * set y get del modulo 15
     */
    public void setModule15access(String mod) {
        this.Modulo[15] = mod;
    }

    public String getModule15access() {
        return this.Modulo[15];
    }

    /*
     * set y get del modulo 16
     */
    public void setModule16access(String mod) {
        this.Modulo[16] = mod;
    }

    public String getModule16access() {
        return this.Modulo[16];
    }

    /*
     * set y get del modulo 17
     */
    public void setModule17access(String mod) {
        this.Modulo[17] = mod;
    }

    public String getModule17access() {
        return this.Modulo[17];
    }

    /*
     * set y get del modulo 18
     */
    public void setModule18access(String mod) {
        this.Modulo[18] = mod;
    }

    public String getModule18access() {
        return this.Modulo[18];
    }

    /*
     * set y get del modulo 19
     */
    public void setModule19access(String mod) {
        this.Modulo[19] = mod;
    }

    public String getModule19access() {
        return this.Modulo[19];
    }

    /*
     * set y get del modulo 20
     */
    public void setModule20access(String mod) {
        this.Modulo[20] = mod;
    }

    public String getModule20access() {
        return this.Modulo[20];
    }

    /*
     * set y get del modulo 21
     */
    public void setModule21access(String mod) {
        this.Modulo[21] = mod;
    }

    public String getModule21access() {
        return this.Modulo[21];
    }

    /*
     * set y get del modulo 22
     */
    public void setModule22access(String mod) {
        this.Modulo[22] = mod;
    }

    public String getModule22access() {
        return this.Modulo[22];
    }

    /*
     * set y get del modulo 23
     */
    public void setModule23access(String mod) {
        this.Modulo[23] = mod;
    }

    public String getModule23access() {
        return this.Modulo[23];
    }

    /*
     * set y get del modulo 24
     */
    public void setModule24access(String mod) {
        this.Modulo[24] = mod;
    }

    public String getModule24access() {
        return this.Modulo[24];
    }

    /*
     * set y get del modulo 25
     */
    public void setModule25access(String mod) {
        this.Modulo[25] = mod;
    }

    public String getModule25access() {
        return this.Modulo[25];
    }

    /*
     * set y get del modulo 26
     */
    public void setModule26access(String mod) {
        this.Modulo[26] = mod;
    }

    public String getModule26access() {
        return this.Modulo[26];
    }

    /*
     * set y get del modulo 27
     */
    public void setModule27access(String mod) {
        this.Modulo[27] = mod;
    }

    public String getModule27access() {
        return this.Modulo[27];
    }

    /*
     * set y get del modulo 28
     */
    public void setModule28access(String mod) {
        this.Modulo[28] = mod;
    }

    public String getModule28access() {
        return this.Modulo[28];
    }

    /*
     * set y get del modulo 29
     */
    public void setModule29access(String mod) {
        this.Modulo[29] = mod;
    }

    public String getModule29access() {
        return this.Modulo[29];
    }

    /*
     * set y get del modulo 30
     */
    public void setModule30access(String mod) {
        this.Modulo[30] = mod;
    }

    public String getModule30access() {
        return this.Modulo[30];
    }

    /*
     * set y get del modulo 31
     */
    public void setModule31access(String mod) {
        this.Modulo[31] = mod;
    }

    public String getModule31access() {
        return this.Modulo[31];
    }

    /*
     * set y get del modulo 32
     */
    public void setModule32access(String mod) {
        this.Modulo[32] = mod;
    }

    public String getModule32access() {
        return this.Modulo[32];
    }

    /*
     * set y get del modulo 33
     */
    public void setModule33access(String mod) {
        this.Modulo[33] = mod;
    }

    public String getModule33access() {
        return this.Modulo[33];
    }

    /*
     * set y get del modulo 34
     */
    public void setModule34access(String mod) {
        this.Modulo[34] = mod;
    }

    public String getModule34access() {
        return this.Modulo[34];
    }

    /*
     * set y get del modulo 35
     */
    public void setModule35access(String mod) {
        this.Modulo[35] = mod;
    }

    public String getModule35access() {
        return this.Modulo[35];
    }

    /*
     * set y get del modulo 36
     */
    public void setModule36access(String mod) {
        this.Modulo[36] = mod;
    }

    public String getModule36access() {
        return this.Modulo[36];
    }

    /*
     * set y get del modulo 37
     */
    public void setModule37access(String mod) {
        this.Modulo[37] = mod;
    }

    public String getModule37access() {
        return this.Modulo[37];
    }

    /*
     * set y get del modulo 38
     */
    public void setModule38access(String mod) {
        this.Modulo[38] = mod;
    }

    public String getModule38access() {
        return this.Modulo[38];
    }

    /*
     * set y get del modulo 39
     */
    public void setModule39access(String mod) {
        this.Modulo[39] = mod;
    }

    public String getModule39access() {
        return this.Modulo[39];
    }

    /*
     * set y get del modulo 40
     */
    public void setModule40access(String mod) {
        this.Modulo[40] = mod;
    }

    public String getModule40access() {
        return this.Modulo[40];
    }

    /*
     * set y get del modulo 41
     */
    public void setModule41access(String mod) {
        this.Modulo[41] = mod;
    }

    public String getModule41access() {
        return this.Modulo[41];
    }

    /*
     * set y get del modulo 42
     */
    public void setModule42access(String mod) {
        this.Modulo[42] = mod;
    }

    public String getModule42access() {
        return this.Modulo[42];
    }

    /*
     * set y get del modulo 43
     */
    public void setModule43access(String mod) {
        this.Modulo[43] = mod;
    }

    public String getModule43access() {
        return this.Modulo[43];
    }

    /*
     * set y get del modulo 44
     */
    public void setModule44access(String mod) {
        this.Modulo[44] = mod;
    }

    public String getModule44access() {
        return this.Modulo[44];
    }

    /*
     * set y get del modulo 45
     */
    public void setModule45access(String mod) {
        this.Modulo[45] = mod;
    }

    public String getModule45access() {
        return this.Modulo[45];
    }

    /*
     * set y get del modulo 46
     */
    public void setModule46access(String mod) {
        this.Modulo[46] = mod;
    }

    public String getModule46access() {
        return this.Modulo[46];
    }

    /*
     * set y get del modulo 47
     */
    public void setModule47access(String mod) {
        this.Modulo[47] = mod;
    }

    public String getModule47access() {
        return this.Modulo[47];
    }

    /*
     * set y get del modulo 48
     */
    public void setModule48access(String mod) {
        this.Modulo[48] = mod;
    }

    public String getModule48access() {
        return this.Modulo[48];
    }

    /*
     * set y get del modulo 49
     */
    public void setModule49access(String mod) {
        this.Modulo[49] = mod;
    }

    public String getModule49access() {
        return this.Modulo[49];
    }

    /*
     * set y get del modulo 50
     */
    public void setModule50access(String mod) {
        this.Modulo[50] = mod;
    }

    public String getModule50access() {
        return this.Modulo[50];
    }

    private String namePackage() {
        Package pack = this.getClass().getPackage();
        return pack.getName();
    }
}
